﻿using DryIoc.ImTools;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ZuesControls.Models;
using ZuesControls.Mvvm;

namespace ZuesControls.Controls
{
   public class ZSListBox:ListBox
    {
        public ICommand NavItemInernalClickCommand { get; }
        public GListItemModel NavigateModelSelect;


        static ZSListBox()
        {

            DefaultStyleKeyProperty.OverrideMetadata(typeof(ZSListBox), new FrameworkPropertyMetadata(typeof(ZSListBox)));
        }
        public ZSListBox()
        {
            this.NavItemInernalClickCommand = new RelayCommand<Tuple<object, MouseButtonEventArgs>>(OnNavItemClick);
        }
        protected override DependencyObject GetContainerForItemOverride()
        {
            return new MyListboxItem(); // 用你的自定义容器替代默认容器
        }

        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            return item is MyListboxItem;
        }

        public ObservableCollection<NavigateModel> NavigateModelList
        {
            get { return (ObservableCollection<NavigateModel>)GetValue(NavigateModelListProperty); }
            set { SetValue(NavigateModelListProperty, value); }
        }

        public static readonly DependencyProperty NavigateModelListProperty =
            DependencyProperty.Register("NavigateModelList", typeof(ObservableCollection<NavigateModel>), typeof(ZSListBox), new PropertyMetadata(null, OnNavigateModelListPropertyChanged));



        public bool IsItemSelected

        {
            get { return (bool)GetValue(IsItemSelectedProperty); }
            set { SetValue(IsItemSelectedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsItemSelectedProperty =
            DependencyProperty.Register("IsItemSelected", typeof(bool), typeof(ZSListBox), new PropertyMetadata(false));



        public ItemType ShowType
        {
            get { return (ItemType)GetValue(ShowTypeProperty); }
            set { SetValue(ShowTypeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsShowGroup.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ShowTypeProperty =
            DependencyProperty.Register("ShowType", typeof(ItemType), typeof(ZSListBox), new PropertyMetadata(ItemType.Gcode,IsShowGroupPropertyChanged));

        private static void IsShowGroupPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            
        }

        private void OnNavItemClick(Tuple<object, MouseButtonEventArgs>? model)
        {
            this.UpdateNavigateModelSelect(model);
        }
        private static void OnNavigateModelListPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

        }

        private void UpdateNavigateModelSelect(Tuple<object, MouseButtonEventArgs>? model)
        {
            GListItemModel clickModel=model.Item1 as GListItemModel;
            var source = this.ItemsSource as IEnumerable<GListItemModel>;
            if (source != null)
            {
                var list = source.ToList();
                foreach (var item in list)
                {
                    item.IsSelect = false;
                }
                clickModel.IsSelect = true;// 转换为 List<MyModel>
            }
            //获取到选择项
          
            this.NavigateModelSelect = clickModel;
        }
        public enum ItemType
        {
            Gcode,
            ExpanderGroup
        }
    }
}
